package com.yugao.fintech.antelope.base.log.event;

import com.yugao.fintech.antelope.base.log.OperateLogInfo;
import com.yugao.fintech.antelope.base.log.enums.BusinessType;
import com.yugao.fintech.antelope.system.SystemApi;
import com.yugao.fintech.antelope.system.model.rqrs.log.OperateLogSaveReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

/**
  异步监听日志事件
 */
@Slf4j
@RequiredArgsConstructor
public class OperateLogListener {

	private final SystemApi systemApi;

	@Async("myTaskScheduler")
	@Order
	@EventListener(OperateLogEvent.class)
	public void saveSysLog(OperateLogEvent event) {
		OperateLogInfo info = (OperateLogInfo) event.getSource();
		OperateLogSaveReq req = new OperateLogSaveReq();

		req.setSummary(info.getSummary());
		req.setTag(info.getTag());
		req.setUserName(info.getUserName());
		BusinessType businessType = BusinessType.valueOf(info.getBusinessType());
		req.setBusinessType(businessType.getCode());
		req.setOperatorType(info.getOperatorType());
		req.setErrorMsg(info.getErrorMsg());
		req.setJsonResult(info.getResponseData());
		req.setTraceId(info.getTraceId());
		req.setRequestIp(info.getIp());
		req.setRequestParam(info.getRequestData());
		req.setRequestMethod(info.getHttpMethod());
		req.setRequestParam(info.getRequestData());
		req.setMethod(info.getClassMethod());
		req.setRequestUrl(info.getUrl());
		req.setCostTime(info.getCostTime());
		req.setLocation("");
		systemApi.saveOperateLog(req);
	}
}
